package com.sample.springbatch.thread;

import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.*;
import org.springframework.batch.core.configuration.JobRegistry;
import org.springframework.batch.core.repository.JobRepository;

import java.time.LocalDate;
import java.util.concurrent.Callable;

/**
 * 创建jobInstance线程
 *
 * @author zwg
 * @date 2023/11/28 16:51
 */
@Slf4j
public class CreateJobInstanceThread implements Callable<String> {

    JobRepository jobRepository;
    String jobName;

    String taskId;

    public CreateJobInstanceThread(JobRepository jobRepository, String jobName,String taskId){
        this.jobRepository = jobRepository;
        this.jobName = jobName;
        this.taskId = taskId;
    }

    public String call(){
        JobParametersBuilder paramsBuilder = new JobParametersBuilder();
        paramsBuilder.addString("jobName",jobName);
        paramsBuilder.addLong("randomTime",System.currentTimeMillis());
        paramsBuilder.addString("date", LocalDate.now().toString());
        paramsBuilder.addString("taskId",taskId);

        JobParameters parameters = paramsBuilder.toJobParameters();

        DefaultJobKeyGenerator jobKeyGenerator = new DefaultJobKeyGenerator();

        /*try {
            JobInstance instance = jobRepository.createJobInstance(jobName,parameters);
        }catch (Exception e){
            log.error("jobRepository.createJobInstance {}",parameters,e);
        }*/

        return jobKeyGenerator.generateKey(parameters);

    }

}
